package com.generator.view.util;

import org.springframework.util.CollectionUtils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * BeanUtils工具 继承org.springframework.beans.BeanUtils
 *
 * @author jaybril
 * @date 2019-04-10 15:42
 **/
public class BeanUtils extends org.springframework.beans.BeanUtils {


    public static <T>T copy(Map map,T targetClass) throws IllegalAccessException {
        Field[] fields = targetClass.getClass().getDeclaredFields();
        for (Field field : fields) {
            int mod = field.getModifiers();
            if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){
                continue;
            }

            field.setAccessible(true);
            Object val=map.get(field.getName());
//                        System.out.println("field.getName():"+field.getName()+",field.getType():"+field.getType());
            if("int".equals(field.getType().toString())){
                field.set(targetClass,Integer.valueOf(val.toString()).intValue());
            }else {
                field.set(targetClass, val);
            }
        }
        return targetClass;
    }

    public static List copyList(List list, Class targetClass) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList();
        }
        List resultList = new ArrayList();
        if(list==null){
            return null;
        }
        list.stream().forEach(item -> {
            try {
                Object object = targetClass.newInstance();
                if(item instanceof HashMap){
//                    Object mapObj=mapToObject((Map)item,Map.class);
//                    BeanUtils.copyProperties(mapObj, object);


                    Field[] fields = object.getClass().getDeclaredFields();
                    for (Field field : fields) {
                        int mod = field.getModifiers();
                        if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){
                            continue;
                        }

                        field.setAccessible(true);
                        Object val=((HashMap) item).get(field.getName());
//                        System.out.println("field.getName():"+field.getName()+",field.getType():"+field.getType());
                        if(val==null){
                            continue;
                        }
                        if("int".equals(field.getType().toString())){
                            field.set(object,Integer.valueOf(val.toString()).intValue());
                        }else {
                            field.set(object, val);
                        }
                    }

                }else {
                    BeanUtils.copyProperties(item, object);
                }

                resultList.add(object);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return resultList;
    }

    public static Object mapToObject(Map map, Class<?> beanClass) throws Exception {
        if (map == null){
            return null;
        }
        Object obj = beanClass.newInstance();

        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            int mod = field.getModifiers();
            if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){
                continue;
            }

            field.setAccessible(true);
            field.set(obj, map.get(field.getName()));
        }

        return obj;
    }
    public static Map<String, Object> objectToMap(Object obj) throws Exception {
        if(obj == null)
            return null;

        Map<String, Object> map = new HashMap<String, Object>();

        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            //默认PropertyDescriptor会有一个class对象，剔除之
            if (key.compareToIgnoreCase("class") == 0) {
                continue;
            }
            Method getter = property.getReadMethod();
            Object value = getter!=null ? getter.invoke(obj) : null;
            map.put(key, value);
        }
        return map;
    }

    public static Map<String, Object> objectToMapWithOutNull(Object obj) throws Exception {
        if(obj == null)
            return null;

        Map<String, Object> map = new HashMap<String, Object>();

        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            //默认PropertyDescriptor会有一个class对象，剔除之
            if (key.compareToIgnoreCase("class") == 0) {
                continue;
            }
            Method getter = property.getReadMethod();
            Object value = getter!=null ? getter.invoke(obj) : null;
            if(value!=null){
                map.put(key, value);
            }
        }
        return map;
    }
}
